package test;


import java.util.Collection;

import com.duniptech.engine.core.modeling.Atomic;
import com.duniptech.engine.core.modeling.Message;
import com.duniptech.engine.core.modeling.api.IMessage;

public class Generator extends Atomic {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Ports
	public static final String stop = "stop";
	public static final String out = "out";
	
	// State
	protected double period;
	protected int count;

	public Generator(String name, double period) {
		super(name);
		addInport(Generator.stop);
		addOutport(Generator.out);
		this.period = period;
		this.setSigma(period);
		count = 0;
	}

	public Generator() {
		this("Generator", 5.0);
	}

	public void deltint() {
		//System.out.println("Start: " + this.getClass().getName() + "::deltint()");
		count++;
		this.setSigma(period);
		//System.out.println("End: " + this.getClass().getName() + "::deltint()");
	}

	public void deltext(double e, IMessage x) {
		//System.out.println("Start: " + this.getClass().getName() + "::deltext()");
		setSigma(getSigma() - e);
		Collection<?> values = x.getValuesOnPort(Generator.stop);
		if(!values.isEmpty()) this.setSigma(INFINITY);
		//System.out.println("End: " + this.getClass().getName() + "::deltext()");
	}

	public IMessage lambda() {
		//System.out.println("Start: " + this.getClass().getName() + "::lambda()");
		Job job = new Job("" + count + "");
		IMessage msg = new Message();
		msg.add(Generator.out, job);
		//System.out.println("End: " + this.getClass().getName() + "::lambda()");
		return msg;
	}

	public double getPeriod() {
		return period;
	}

	public void setPeriod(double period) {
		this.period = period;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

}

